<?php

/**
 * Framework_Presenter_Smarty
 *
 * @author      Joe Stump <joe@joestump.net>
 * @copyright   (c) 2006 Joseph C. Stump. All rights reserved.
 * @license     http://www.opensource.org/licenses/bsd-license.php
 * @package     Framework
 * @subpackage  Presenter
 * @filesource
 */


/**
 * Framework_Presenter_Smarty
 *
 * By default we use Smarty as our websites presentation layer (view). Smarty
 * is a robust compiling template engine with an active community.
 *
 * @author      Joe Stump <joe@joestump.net>
 * @package     Framework
 * @subpackage  Presenter
 * @link        http://smarty.php.net
 */
class Framework_Presenter_Smarty extends Framework_Presenter_Common
{
	/**
	 * $template
	 *
	 * The templating system, such as SMARTY
	 *
	 * @access      protected
	 * @var         object      $template       Instance of Smarty
	 */
	protected $template = null;

	/**
	 * $cacheInit
	 *
	 * @access 		protected
	 * @var 		array 		$cacheInit		Whether or not caching is initialized
	 */
	protected $cacheInit = false;
	
	/**
	 * __construct
	 *
	 * @access      public
	 * @param       object      $module         Instance of Framework_Module
	 * @return      void
	 */
	public function __construct(Framework_Module $module)
	{
		parent::__construct($module);
		
		// Check for custom templat/skin
		$owner = Framework_Owner::singleton();
		
		if ($owner->isValid && $owner->skin) {
			$skin = $owner->skin;
		} else {
			$skin = 'Default';
		}
		
		$this->template = Framework_Template::factory('Smarty', $module->moduleName, $skin);
	}


	/**
     * cacheInit
     *
     * Initialized caching
     *
     * @access      public
     * @return      boolean
     */
	public function cacheInit()
	{

		$this->template->caching = 1;
		$this->template->cache_lifetime = 7200;
		$this->template->compile_check = true;
			
		if($this->template->is_cached($this->module->templateCache['pageTemplate'],Framework::$request->ownerName.'|'.$this->module->moduleName.'|'.$this->module->templateCache['pageTemplate'].'|'.$this->module->templateCache['id'])) {
			$this->cacheInit = true;
			return true;
		} else {
			return false;
		}




	}
	
	/**
     * clearCache
     *
     * Clears the template cache
     *
     * @access      public
     * @return      void
     */
	public function clearCache()
	{
		$this->template->clear_cache(null, Framework::$request->ownerName.'|'.$this->module->moduleName.'|'.$this->module->templateCache['pageTemplate']);
	}

	/**
	 * display
	 *
	 * @access public
	 * @return void
	 */
	public function display()
	{

		
		$this->template->assign('site',Framework::$site);
		$this->template->assign('tplFile',$this->module->tplFile);
		$this->template->assign('user',$this->user);
		$this->template->assign('session',$this->session);
		$this->template->assign('filePath',Framework::$request->getBaseHTTPPath(false));
		$this->template->assign('uriPath',Framework::$request->getBaseHTTPPath());
		$this->template->assign('request',Framework::$request);


		//$this->template->assign('breadcrumb', $this->module->getBreadcrumb());
		if ($this->module->ran) {
			$path = Framework_Template::getPath($this->module->tplFile, $this->module->moduleName);
			$this->template->assign('modulePath', $path);
			foreach ($this->module->getData() as $var => $val) {
				if (!in_array($var,array('path','tplFile'))) {
					$this->template->assign($var,$val);
				}
			}

			// Check to see if module has form objects
			// if so, render them to array and assign them
			if (count($this->module->forms)){
				require_once "HTML/QuickForm/Renderer/ArraySmarty.php";
				$renderer = new HTML_QuickForm_Renderer_ArraySmarty($this->template);
				foreach ($this->module->forms as $var => $val){
					$val->accept($renderer);
					$this->template->assign($var,$renderer->toArray());
				}
			}

			if ($this->module->pageTemplateFile == null) {
				$this->module->pageTemplateFile = 'Page.tpl';
			}

			$pageTemplateFile = $this->module->pageTemplateFile;
				
		} 
		
		if (!is_null($this->module->templateCache)) {
//			if ($this->module->ran) {
//				echo 'ran';
//			}
			$this->template->display($this->module->templateCache['pageTemplate'], Framework::$request->ownerName.'|'.$this->module->moduleName.'|'.$this->module->templateCache['pageTemplate'].'|'.$this->module->templateCache['id']);
		} else {
			$this->template->display($pageTemplateFile);
		}
		
	}
}

?>
